﻿using LineOfBusiness.Controls.Base;
using LineOfBusiness.DataAccess;
using LineOfBusiness.Poco;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace LineOfBusiness.MO.Controllers
{
    public class ModelsController : LOBController
    {
        public ModelsController()
        {
            this.GridViewName = "grvtblModels";
            this.FormName = "tblModels";
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Add()
        {
            ViewBag.Edit = 0;
            MO_tblModels Models = new MO_tblModels();
            return View("Input", Models);
        }


        public ActionResult GetModel(string Mode,string ItemID)
        {
            using (EFDALContainer dal = new EFDALContainer())
            {
                MO_tblModels Models = dal.Repository.GetQuery<MO_tblModels>().Where(o => o.ModelType == Mode && o.ItemID == ItemID).FirstOrDefault();

                if (Models == null)
                {
                    ViewBag.Edit = 0;
                    Models = new MO_tblModels() { ItemID = ItemID, ModelType = Mode };
                }
                else
                    ViewBag.Edit = 1;

                return View("Input", Models);
            }
        }

        public ActionResult View(string ModelID)
        {
            using (EFDALContainer dal = new EFDALContainer())
            {
                MO_tblModels Models = dal.Repository.GetQuery<MO_tblModels>().Where(o => o.ModelID == ModelID).FirstOrDefault();
                ViewBag.Edit = -1;
                return View("Input", Models);
            }
        }

        public ActionResult Edit(string ModelID)
        {
            using (EFDALContainer dal = new EFDALContainer())
            {
                MO_tblModels Models = dal.Repository.GetQuery<MO_tblModels>().Where(o => o.ModelID == ModelID).FirstOrDefault();
                if (this.Request.QueryString.AllKeys.Contains("View"))
                    ViewBag.Edit = -1;
                else
                    ViewBag.Edit = 1;

                return View("Input", Models);
            }

        }

        [HttpPost]
        public ActionResult Delete(MO_tblModels Models)
        {
            using (EFDALContainer dal = new EFDALContainer())
            {
                try
                {
                    var item = dal.Repository.GetOne<IV_tblItemLists>(o => o.ItemID == Models.ItemID );

                    if(item!=null)
                    {
                        if (Models.ModelType == "1")
                            item.ModelID2D = null;
                        if (Models.ModelType == "2")
                            item.ModelID3D = null;
                        if (Models.ModelType == "3")
                            item.Mold = null;

                        dal.Repository.Update(item);
                    }

                    
                    dal.Repository.Delete(Models);
                    dal.UnitOfWork.SaveChanges();
                }
                catch (Exception exp)
                {
                    return Json(exp.Message);
                }
                return Json("0");
            }
        }

        [HttpPost]
        public ActionResult Save(MO_tblModels Models)
        {
            string method = "Edit";
            string KeyOld = "";

            if (ViewData["Edit"].Equals("0"))
            {
                method = "Add";
                Models.CreatedDate = DateTime.Now;
                Models.UserID = LoginUser.UserID;
            }
            else
            {
                KeyOld = ViewData["ID"] as string;
            }

            using (EFDALContainer dal = new EFDALContainer())
            {
                try
                {
                    if (method == "Add")
                    {
                        
                        dal.Repository.Add(Models);
                    }
                    else
                    {
                        if (KeyOld == Models.ModelID)
                        {
                            dal.Repository.Update(Models);
                        }
                        else
                        {
                            dal.Repository.Delete<MO_tblModels>(o => o.ModelID == KeyOld);
                            dal.Repository.Add(Models);
                        }
                    }

                    var ModelsOld = dal.Repository.GetOne<MO_tblModels>(o => o.ModelID == KeyOld);
                    if (ModelsOld!=null)
                    {
                        var itemOld = dal.Repository.GetOne<IV_tblItemLists>(o => o.ItemID == ModelsOld.ItemID);

                        if (itemOld != null)
                        {
                            if (ModelsOld.ModelType == "1")
                                itemOld.ModelID2D = null;
                            if (ModelsOld.ModelType == "2")
                                itemOld.ModelID3D = null;
                            if (ModelsOld.ModelType == "3")
                                itemOld.Mold = null;

                            dal.Repository.Update(itemOld);
                        }
                }

                    var item = dal.Repository.GetOne<IV_tblItemLists>(o => o.ItemID == Models.ItemID);

                    if (item != null)
                    {
                        if (Models.ModelType == "1")
                            item.ModelID2D = Models.ModelID;
                        if (Models.ModelType == "2")
                            item.ModelID3D = Models.ModelID;
                        if (Models.ModelType == "3")
                            item.Mold = Models.ModelID;

                        dal.Repository.Update(item);
                    }

                    dal.UnitOfWork.SaveChanges();
                }
                catch (Exception exp)
                {
                    return Json(exp.Message);
                }
                return Json("0");
            }
        }

    }
}
